package com.marco.statics.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.marco.statics.domain.HeatMap;
import com.marco.statics.domain.MapInfo;
import com.marco.statics.domain.Point;
import com.marco.statics.service.TestDataService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * TestDataServiceImpl.java
 * Create on 2017/4/11
 * Copyright(c) JD Inc 2017. All Rights Reserved.
 * <pre>
 * 功能说明：
 * </pre>
 */
@Service
public class TestDataServiceImpl implements TestDataService {
    private static final Logger logger = LoggerFactory.getLogger(TestDataServiceImpl.class);
    private static ObjectMapper jsonParser = new ObjectMapper();
    private static final String FILE_PATH = "E:\\数据组需求\\新需求\\地域维度";
    //TODO 渗透率除去了100，其他的要去掉

    @Override
    public MapInfo getTestData(Integer colum) {
        MapInfo mapInfo = new MapInfo();
        LineIterator iterator = null;
        Double min = 0D;
        Double max = 0D;
        String line = null;
        try {
            List<Point> points = Lists.newArrayList();
            iterator = FileUtils.lineIterator(new File(FILE_PATH, "地域_new.txt"));
            mapInfo.setTitle(iterator.nextLine().split("\t")[colum - 2]);
            while (iterator.hasNext()) {
                Point point = new Point();
                line = iterator.nextLine();
                String[] strs = line.split("\t");
                String name = strs[2].trim();
                if (StringUtils.isNotBlank(strs[3])) {
                    name = strs[3].trim();
                }
                point.setName(name);
                Double count = Double.parseDouble(strs[colum].trim());
                if (count > max) {
                    max = count;
                }
                if (count < min) {
                    min = count;
                }
                point.setValue(new String[]{strs[0].trim(), strs[1].trim(), strs[colum].trim()});
                points.add(point);
            }
            Collections.sort(points, new Comparator<Point>() {
                @Override
                public int compare(Point o1, Point o2) {
                    Double l1 = Double.parseDouble(o1.getValue()[2]);
                    Double l2 = Double.parseDouble(o2.getValue()[2]);
                    return l1.compareTo(l2);
                }
            });
            mapInfo.setMax(max);
            mapInfo.setMin(min);
            mapInfo.setPoints(points);
            logger.info("数据处理完毕,所有点数量为 {} 最大值为 {} 最小值为 {}", points.size(), max, min);
        } catch (Exception e) {
            logger.error("抛出异常 {}", line, e);
        } finally {
            LineIterator.closeQuietly(iterator);
        }
        return mapInfo;
    }

    @Override
    public MapInfo getHeatMapData(Integer colum) {
        MapInfo mapInfo = new MapInfo();
        LineIterator iterator = null;
        String line = null;
        try {
            iterator = FileUtils.lineIterator(new File(FILE_PATH, "城镇POP_new.txt"));
            String title = iterator.nextLine().split("\t")[colum];
            List<HeatMap> heatMapList = Lists.newArrayList();
            while (iterator.hasNext()) {
                HeatMap heatMap = new HeatMap();
                line = iterator.nextLine();
                String[] strs = line.split("\t");
                Long count = Long.parseLong(strs[colum].trim());
                heatMap.setLng(Double.parseDouble(strs[0].trim()));
                heatMap.setLat(Double.parseDouble(strs[1].trim()));
                heatMap.setCount(count);
                heatMapList.add(heatMap);
            }
            Collections.sort(heatMapList, new Comparator<HeatMap>() {
                @Override
                public int compare(HeatMap o1, HeatMap o2) {
                    Double aDouble = o1.getCount() * 1000D;
                    Double bDouble = o2.getCount() * 1000D;
                    return aDouble.compareTo(bDouble);
                }
            });
            Integer size = heatMapList.size();
            mapInfo.setTitle(title);
//            mapInfo.setMax(heatMapList.get(size - 1).getCount());
//            mapInfo.setMin(heatMapList.get(0).getCount());
            List<String> data = Lists.newArrayList();
            for (HeatMap heatMap : heatMapList) {
                data.add(jsonParser.writeValueAsString(heatMap));
            }
            mapInfo.setDataLists(data);
            logger.info("index : {} 最大值为 {} ,最小值为 {} ", colum, mapInfo.getMax(), mapInfo.getMin());
            return mapInfo;
        } catch (Exception e) {
            logger.error("获取数据出现异常 {} {}", line, e.getMessage());
        } finally {
            LineIterator.closeQuietly(iterator);
        }
        return mapInfo;
    }
}
